package com.geline.mybatisplus.handler.impl;

import cn.hutool.core.util.StrUtil;
import com.geline.mybatisplus.annotation.LogCarry;
import com.geline.mybatisplus.handler.LogCarryHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;

/**
 * 打印日志收集信息
 *
 * @author: mx
 * @date: 2024-08-17 15:09
 */
@Slf4j
public class DefaultLogCarryHandler implements LogCarryHandler {

    @Override
    public void handleInsert(LogCarry logCarry, Object entity) {
        BeanMap beanMap = BeanMap.create(entity);
        Object dataId = beanMap.get(logCarry.dataId());
        String dataName = StrUtil.isNotBlank(logCarry.dataName()) ? (String)beanMap.get(logCarry.dataName()) : null;
        Object childId = StrUtil.isNotBlank(logCarry.childId()) ? beanMap.get(logCarry.childId()) : null;
        String childName = StrUtil.isNotBlank(logCarry.childName()) ? (String)beanMap.get(logCarry.childName()) : null;
        log.info("logCarry : {} {} {} {} {}", logCarry.title(), dataId, dataName, childId, childName);
    }

    @Override
    public void handleUpdate(LogCarry logCarry, Object entity) {

    }

    @Override
    public void handleDelete(LogCarry logCarry, Object entity) {

    }
}
